package model.altarica_fields;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import model.AltaRicaNode;

public class Var {
	
	private String id;
	private List<String> subs;
	private VarType type;
	private String attribute;
	private boolean stateVar;

	private Var remoteVar;
	private AltaRicaNode node;
	private LinkedList<AltaRicaNode> remoteNodes;
	
	public static final String ATTR_PUBLIC = "PUBLIC";
	public static final String ATTR_PRIVATE = "PRIVATE";
	public static final String ATTR_PARENT = "PARENT";
	
	
	public Var(String id, VarType type,  boolean stateVar, String attribute) {
			
		this.id = id;
		this.subs = null;
		this.type = type;
		
		this.stateVar = stateVar;
		this.attribute = attribute;
		this.remoteVar = null;
		
		this.node = null;
		this.remoteNodes = new LinkedList<AltaRicaNode>();
	}
	
	public Var(List<String> ids, VarType type,  boolean stateVar, String attribute) {
		
		this.id = ((ArrayList<String>) ids).get(ids.size() - 1);
		
		ids.remove(ids.size() - 1);
		
		this.subs = null;
		
		if (ids.isEmpty() == false) {
			this.subs = new LinkedList<String>();
		}
		
		for (String str : ids) {
			((LinkedList<String>) subs).addLast(str);
		}
		
		this.type = type;
		
		this.stateVar = stateVar;
		this.attribute = attribute;
		this.remoteVar = null;
		
		this.node = null;
		this.remoteNodes = new LinkedList<AltaRicaNode>();
	}
	
	public String getId() {
		
		String str = new String();
		
		if (subs != null) {
			for (String node : subs) {
				str += node + ".";
			}
		}
		
		str += this.id;
		
		return str;
	}
	
	public String getName() {
		
		return this.id;
	}
	
	public void setNode(AltaRicaNode node) {
		
		this.node = node;
	}
	
	public AltaRicaNode getNode() {
		
		return this.node;
	}
	
	public void addRemoteReference(AltaRicaNode rnode) {
		
		this.remoteNodes.add(rnode);
	}
	
	public List<AltaRicaNode> getRemoteReference() {
		
		return this.remoteNodes;
	}
	
	public boolean isStateVar() {
		
		return this.stateVar;
	}
	
	public boolean isPublic() {
		
		return this.attribute == ATTR_PUBLIC;
	}
	
	public boolean isPrivate() {
		
		return this.attribute == ATTR_PRIVATE;
	}

	public boolean isParent() {
	
		return this.attribute == ATTR_PARENT;
	}
	
	public boolean isLocal() {
		
		return this.subs == null;
	}
	
	public List<String> getSubs() {
		
		return this.subs;
	}
	
	public void setRemoteVar(Var var) {
		
		this.remoteVar = var;
		
		this.type = var.type;
		this.attribute = var.attribute;
		this.stateVar = var.stateVar;
	}
	
	public Var getRemoteVar() {
		
		return this.remoteVar;
	}
	
	public VarType getType() {
		
		return this.type;
	}
	
	public void setType(VarType type) {
		
		this.type = type;
	}
	
	public String toString(){
		
		String str = new String();
				
		if (subs != null) {
			for (String node : subs) {
				str += node + ".";
			}
		}
		
		str +=  this.id;
		
		if (type != null)
			str += " : " + type.toString() + " : " + attribute;
		
		return str;
	}
}
